package com.shiyi.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shiyi.common.ResponseResult;
import com.shiyi.entity.WebConfig;
import com.shiyi.mapper.WebConfigMapper;
import com.shiyi.service.RedisService;
import com.shiyi.service.WebConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;


import static com.shiyi.common.SqlConf.LIMIT_ONE;

/**
 * <p>
 * 网站配置表 服务实现类
 *
 * </p>
 *
 * @author blue
 * @since 2021-11-27
 */
@Service
@RequiredArgsConstructor
public class WebConfigServiceImpl extends ServiceImpl<WebConfigMapper, WebConfig> implements WebConfigService {


    private final RedisService redisService;

    /**
     * 网站配置
     * @return
     */
    @Override
    public ResponseResult listWebConfig() {
        WebConfig webConfig = baseMapper.selectOne(new QueryWrapper<WebConfig>()
                .last(LIMIT_ONE));
        //从缓存中将信息取出来
        Object redisObject = redisService.getCacheObject("wechat-webconfig");
        if(redisObject!=null){
            //缓存中有数据
            JSONObject jsonObject = (JSONObject) redisObject;
            webConfig.setZnwms(jsonObject.getString("znwms"));
            webConfig.setTxkey(jsonObject.getString("txkey"));
            webConfig.setGroupChats(jsonObject.getString("groupChats"));
            webConfig.setNameChats(jsonObject.getString("nameChats"));
        }
        return ResponseResult.success(webConfig);
    }

    /**
     * 修改网站配置
     * @param webConfig
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateWebConfig(WebConfig  webConfig) {
        try {
            //正常修改的流程
            baseMapper.updateById(webConfig);
            //将只能微秘书的东西放在缓存中
            JSONObject object = new JSONObject();
            if(Strings.isNotBlank(webConfig.getZnwms())){
                object.put("znwms",webConfig.getZnwms());
            }
            if(Strings.isNotBlank(webConfig.getTxkey())){
                object.put("txkey",webConfig.getTxkey());
            }
            if(Strings.isNotBlank(webConfig.getGroupChats())){
                object.put("groupChats",webConfig.getGroupChats());
            }
            if(Strings.isNotBlank(webConfig.getNameChats())){
                object.put("nameChats",webConfig.getNameChats());
            }
            //将配置信息放在缓存中
            redisService.setCacheObject("wechat-webconfig",object);

        }catch (Exception e){
            e.printStackTrace();
        }

        return ResponseResult.success();
    }

    /**
     * 更新缓存
     * @return
     */
    @Override
    public ResponseResult reloadRedis(WebConfig  webConfig) {
        try {
            //将只能微秘书的东西放在缓存中
            JSONObject object = new JSONObject();
            if(Strings.isNotBlank(webConfig.getZnwms())){
                object.put("znwms",webConfig.getZnwms());
            }
            if(Strings.isNotBlank(webConfig.getTxkey())){
                object.put("txkey",webConfig.getTxkey());
            }
            if(Strings.isNotBlank(webConfig.getGroupChats())){
                object.put("groupChats",webConfig.getGroupChats());
            }
            if(Strings.isNotBlank(webConfig.getNameChats())){
                object.put("nameChats",webConfig.getNameChats());
            }
            //将配置信息放在缓存中
            redisService.setCacheObject("wechat-webconfig",object);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseResult.success();
    }

    /**
     * 获取缓存中的值，用于检查缓存是否更新成功 wechat-webconfig
     * @return
     */
    @Override
    public ResponseResult getRedisObject(String  redisKeus) {
        Object redisObject = redisService.getCacheObject(redisKeus);
        return ResponseResult.success(redisObject);
    }


}
